package com.sbpm.crmbpm.client.grid;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.sbpm.crmbpm.client.providers.AddressProperties;
import com.sbpm.crmbpm.client.providers.CompanyProperties;
import com.sbpm.crmbpm.shared.data.client.*;
import com.sbpm.crmbpm.shared.data.dict.City;
import com.sbpm.crmbpm.shared.data.dict.Country;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;

import java.util.ArrayList;
import java.util.List;

public class AddressColumnModelFactory
{
    public static final AddressProperties PROPS = GWT.create( AddressProperties.class );

    private static ColumnConfig<Address, Long> idCol = new ColumnConfig<Address, Long>( PROPS.id(), 100, "ID");
    private static ColumnConfig<Address, String> addressCol = new ColumnConfig<Address, String>( PROPS.address(), 300, "Адрес");
    private static ColumnConfig<Address, AddressType> addressTypeCol = new ColumnConfig<Address, AddressType>( PROPS.type(), 200, "Тип");
    private static ColumnConfig<Address, City> cityCol = new ColumnConfig<Address, City>( PROPS.city(), 100, "Город" );
    private static ColumnConfig<Address, Country> countryCol = new ColumnConfig<Address, Country>( PROPS.country(), 150, "Страна");
//    private static ColumnConfig<Address, City> countryEditableCol = new ColumnConfig<Address, City>( PROPS.city(), 150, "Страна");


    public static ColumnModel<Address> createAddressColumnModel()
    {
        idCol.setHidden( true );
        addressTypeCol.setCell( new AbstractCell<AddressType>()
        {
            @Override
            public void render( Context context, AddressType value, SafeHtmlBuilder sb )
            {
                if( value != null )
                {
                    sb.appendEscaped( value.getType() );
                }
            }
        } );
        cityCol.setCell( new AbstractCell< City >()
        {
            @Override
            public void render( Context context, City value, SafeHtmlBuilder sb )
            {
                if( value != null )
                {
                    sb.appendEscaped( value.getName() );
                }
            }
        } );
        countryCol.setCell( new AbstractCell< Country >()
        {
            @Override
            public void render( Context context, Country value, SafeHtmlBuilder sb )
            {
                if( value != null )
                {
                    sb.appendEscaped( value.getName() );
                }
            }
        } );

        List<ColumnConfig<Address, ?>> columnConfigs = new ArrayList<ColumnConfig<Address, ?>>();
        columnConfigs.add( idCol );
        columnConfigs.add( addressTypeCol );
        columnConfigs.add( countryCol );
        columnConfigs.add( cityCol );
        columnConfigs.add( addressCol );
        ColumnModel<Address> columnModel = new ColumnModel<Address>(columnConfigs);
        return columnModel;
    }

    public static AddressProperties getProps()
    {
        return PROPS;
    }

    public static ColumnConfig< Address, Long > getIdCol()
    {
        return idCol;
    }

    public static ColumnConfig< Address, String > getAddressCol()
    {
        return addressCol;
    }

    public static ColumnConfig< Address, AddressType > getAddressTypeCol()
    {
        return addressTypeCol;
    }

    public static ColumnConfig< Address, City > getCityCol()
    {
        return cityCol;
    }

    public static ColumnConfig< Address, Country > getCountryCol()
    {
        return countryCol;
    }
}
